﻿using System.IO;
using log4net.Config;
using log4net;

namespace Server.Tools
{
    /// <summary>
    /// 这是一个Singleton类，旨在为全局的调试提供日志记录
    /// </summary>
    public sealed class LogService
    {
        /// <summary>
        /// 唯一实例
        /// </summary>
        private static LogService instance =  new LogService();

        /// <summary>
        /// 私有的构造函数，加载log4net配置文件
        /// </summary>
        private LogService()
        {
            isShowOnConsole = Properties.Settings.Default.IsLogOnConsole;
            XmlConfigurator.Configure(new FileInfo("log4net.cfg.xml"));
        }

        /// <summary>
        /// 获取唯一实例的属性
        /// </summary>
        public static LogService Instance
        {
            get
            {
                if (instance.isShowOnConsole) instance.log = LogManager.GetLogger("logAll");
                else instance.log = LogManager.GetLogger("logExceptConsole");
                return instance;
            }
        }

        private bool isShowOnConsole;
        /// <summary>
        /// 该字段标识着日志记录是否显示在控制台上
        /// </summary>
        public bool IsShowOnConsole
        {
            get { return IsShowOnConsole; }
            set { isShowOnConsole = value; }
        }

        private ILog log;
        /// <summary>
        /// 直接进行日志操作的实例
        /// </summary>
        public ILog Log
        {
            get
            {
                return log;
            }
            set{ log = value;}
        }
    }
}